package com.gxa.weixing.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.Shop;
import com.gxa.weixing.pc.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2022/01/06  11:16:37
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "门店管理Api")
public class ShopController {

    @Resource
    private ShopService shopService;

    /**
     * 根据门店名称(可以为空,空则全查询)获取门店列表的接口
     *
     * @param name 门店名称
     * @param page 页码  如果不传递则为1 商品选择界面调用需要
     * @param size 每页数据量 如果不传递则为100000 商品选择界面调用需要
     * @return 返回查询的数据
     */
    @PostMapping("/getList")
    @ApiOperation("根据门店名称(可以为空,空则全查询)获取门店列表的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "门店名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer")
    })
    public Map<String, Object> getList(String name, Integer page, Integer size) throws BusinessException {
        PageInfo<Shop> list = shopService.getList(name, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(list.getList());
        jsonResult.put("count", list.getTotal());
        return jsonResult.getDataMap();
    }

    /**
     * 修改门店接口
     *
     * @param shop 修改门店对象
     * @return 返回是否成功
     */
    @PostMapping("/update")
    @ApiOperation("修改门店接口")
    @ApiImplicitParam(name = "shop", value = "shop", paramType = "body")
    public Map<String, Object> update(@RequestBody Shop shop) throws BusinessException {
        shopService.update(shop);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }


    /**
     * 删除门店接口
     *
     * @param id 删除门店的编号
     * @return 返回是否成功
     */
    @PostMapping("/delete")
    @ApiOperation("删除门店接口")
    @ApiImplicitParam(name = "id", value = "编号", paramType = "query")
    public Map<String, Object> delete(@RequestParam("id") Long id) throws BusinessException {
        shopService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    /**
     * 添加门店接口
     *
     * @param shop 门店对象
     * @return 返回是否成功
     */
    @PostMapping("/add")
    @ApiOperation("添加门店接口")
    @ApiImplicitParam(name = "shop", value = "shop", paramType = "body")
    public Map<String, Object> add(@RequestBody Shop shop) throws BusinessException {
        shopService.add(shop);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

}
